package com.fyzk.activity.trend;

import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static com.tyjc.unique._AnalaysesBaseFragment.TAG;

/**
 * Created by fanxs11 on 2017/7/16.
 */

public class TrendBean {


    protected List<IssuesBean> issues;
    protected List<MissesBean> misses;
    protected List<HcsBean> hcs;
    protected List<BasicMissesBean> basicMisses;

    public String[] maxMiss = new String[4];

    public List<IssuesBean> getIssues() {
        return issues;
    }

    public void setIssues(List<IssuesBean> issues) {
        Collections.reverse(issues);
        this.issues = issues;
    }

    public List<MissesBean> getMisses() {
        return misses;
    }

    public void setMisses(List<MissesBean> misses) {
        Collections.reverse(misses);
        this.misses = misses;
    }

    public List<HcsBean> getHcs() {
        return hcs;
    }

    public void setHcs(List<HcsBean> hcs) {
//        Collections.reverse(hcs);
        this.hcs = hcs;
    }

    public List<BasicMissesBean> getBasicMisses() {
        return basicMisses;
    }

    public void setBasicMisses(List<BasicMissesBean> basicMisses) {
        Collections.reverse(basicMisses);
        this.basicMisses = basicMisses;
    }

    public static class IssuesBean {
        /**
         * id : 169513
         * lotteryType : 2008
         * phase : 17073142
         * endtime : 2017-07-31 16:39:00
         * createtime : 2017-07-31 16:39:00
         * status : 3
         * wincode : 01,07,10,04,05
         * andValue : 27
         * spanValue : 9
         * bigsmall : 2:3
         * oddEvenValue : 3:2
         * singleDoubleValue :
         * primeValue : 3:2
         */

        private int id;
        private int lotteryType;
        private String phase;
        private String endtime;
        private String createtime;
        private int status;
        private String wincode;
        private String andValue;
        private String spanValue;
        private String bigsmall;
        private String oddEvenValue;
        private String singleDoubleValue;
        private String primeValue;

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public int getLotteryType() {
            return lotteryType;
        }

        public void setLotteryType(int lotteryType) {
            this.lotteryType = lotteryType;
        }

        public String getPhase() {
            return phase;
        }

        public void setPhase(String phase) {
            this.phase = phase;
        }

        public String getEndtime() {
            return endtime;
        }

        public void setEndtime(String endtime) {
            this.endtime = endtime;
        }

        public String getCreatetime() {
            return createtime;
        }

        public void setCreatetime(String createtime) {
            this.createtime = createtime;
        }

        public int getStatus() {
            return status;
        }

        public void setStatus(int status) {
            this.status = status;
        }

        public String getWincode() {
            if (wincode != null && wincode.contains(",")) {
                wincode = wincode.replace(",", " ");
            }
            return wincode;
        }

        public void setWincode(String wincode) {
            this.wincode = wincode;
        }

        public String getAndValue() {
            return andValue;
        }

        public void setAndValue(String andValue) {
            this.andValue = andValue;
        }

        public String getSpanValue() {
            return spanValue;
        }

        public void setSpanValue(String spanValue) {
            this.spanValue = spanValue;
        }

        public String getBigsmall() {
            return bigsmall;
        }

        public void setBigsmall(String bigsmall) {
            this.bigsmall = bigsmall;
        }

        public String getOddEvenValue() {
            return oddEvenValue;
        }

        public void setOddEvenValue(String oddEvenValue) {
            this.oddEvenValue = oddEvenValue;
        }

        public String getSingleDoubleValue() {
            return singleDoubleValue;
        }

        public void setSingleDoubleValue(String singleDoubleValue) {
            this.singleDoubleValue = singleDoubleValue;
        }

        public String getPrimeValue() {
            return primeValue;
        }

        public void setPrimeValue(String primeValue) {
            this.primeValue = primeValue;
        }
    }

    public static class MissesBean {
        /**
         * id : 150112
         * lotteryType : 2008
         * phase : 17073142
         * hotCount : 0
         * type : 1
         * name : 2008MV_RX
         * value : {"key":"2008MV_RX","lotteryType":2008,"miss":[0,3,3,0,0,2,0,5,1,0,1]}
         * wincode : 01,07,10,04,05
         */

        private int id;
        private int lotteryType;
        private String phase;
        private int hotCount;
        private int type;
        private String name;
        private String value;
        private String wincode;
        public int maxMiss = 0;

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public int getLotteryType() {
            return lotteryType;
        }

        public void setLotteryType(int lotteryType) {
            this.lotteryType = lotteryType;
        }

        public String getPhase() {
            return phase;
        }

        public void setPhase(String phase) {
            this.phase = phase;
        }

        public int getHotCount() {
            return hotCount;
        }

        public void setHotCount(int hotCount) {
            this.hotCount = hotCount;
        }

        public int getType() {
            return type;
        }

        public void setType(int type) {
            this.type = type;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }

        public String getWincode() {
            return wincode;
        }

        public void setWincode(String wincode) {
            this.wincode = wincode;
        }

        public List<Integer> missValues = new ArrayList<>();
        /**
         * 获取彩种当前玩法的遗漏值集合
         * @param missTitle    玩法对应的标签（如11x5 任选：missTitle = "miss"，前一：missTitle = "ge"）
         * @return
         */
        public List<Integer> getMissValues(String missTitle) {

            if (value != null && !value.equals("") && missTitle != null) {
                try {
                    JSONObject obj = new JSONObject(value);
                    if (obj.has(missTitle)) {
                        String missValue = obj.getString(missTitle);
                        missValue =  missValue.subSequence(1, missValue.length()-1).toString();
                        String[] missArray = missValue.split(",");
                        missValues = strZhuanInteger(missArray);
                        for (int i= 0; i < missValues.size(); i++) {
                            if (maxMiss < missValues.get(i)) {
                                maxMiss = missValues.get(i);
                            }
                        }
                    }

                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }


            return missValues;
        }

    }

    public static class HcsBean {
        /**
         * id : 150128
         * lotteryType : 2008
         * phase : 17073142
         * hotCount : 30
         * type : 2
         * name : 2008HC_RX
         * value : {"hc":[16,14,14,12,14,14,10,13,15,14,14],"key":"2008HC_RX","lotteryType":2008}
         * wincode : 01,07,10,04,05
         */

        private int id;
        private int lotteryType;
        private String phase;
        private int hotCount;
        private int type;
        private String name;
        private String value;
        private String wincode;
        public int maxHc = 0;

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public int getLotteryType() {
            return lotteryType;
        }

        public void setLotteryType(int lotteryType) {
            this.lotteryType = lotteryType;
        }

        public String getPhase() {
            return phase;
        }

        public void setPhase(String phase) {
            this.phase = phase;
        }

        public int getHotCount() {
            return hotCount;
        }

        public void setHotCount(int hotCount) {
            this.hotCount = hotCount;
        }

        public int getType() {
            return type;
        }

        public void setType(int type) {
            this.type = type;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }

        public String getWincode() {
            return wincode;
        }

        public void setWincode(String wincode) {
            this.wincode = wincode;
        }

        public List<Integer> hcInfors = new ArrayList<>();
        public List<Integer> getHcValues(String hcTitle) {
            if (value != null && !value.equals("")) {
                try {
                    JSONObject obj = new JSONObject(value);
                    if (obj.has(hcTitle)) {
                        String hcValue = obj.getString(hcTitle);
                        hcValue =  hcValue.subSequence(1, hcValue.length()-1).toString();
                        String[] hcArray = hcValue.split(",");
                        hcInfors = strZhuanInteger(hcArray);
                        for (int i= 0; i < hcInfors.size(); i++) {
                            if (maxHc < hcInfors.get(i)) {
                                maxHc = hcInfors.get(i);
                            }
                        }
                    }

                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            return hcInfors;
        }
    }

    public static class BasicMissesBean {
        /**
         * id : 466588
         * lotteryType : 1009
         * phase : 170814036
         * hotCount : 0
         * type : 1
         * name : 1009MV_00
         * value : {"key":"1009MV_00","lotteryType":1009,"miss":[15,0,2,0,0,1]}
         * wincode : 2,4,5
         */

        private int id;
        private int lotteryType;
        private String phase;
        private int hotCount;
        private int type;
        private String name;
        private String value;
        private String wincode;

        public int maxMiss = 0;

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public int getLotteryType() {
            return lotteryType;
        }

        public void setLotteryType(int lotteryType) {
            this.lotteryType = lotteryType;
        }

        public String getPhase() {
            return phase;
        }

        public void setPhase(String phase) {
            this.phase = phase;
        }

        public int getHotCount() {
            return hotCount;
        }

        public void setHotCount(int hotCount) {
            this.hotCount = hotCount;
        }

        public int getType() {
            return type;
        }

        public void setType(int type) {
            this.type = type;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }

        public String getWincode() {
            return wincode;
        }

        public void setWincode(String wincode) {
            this.wincode = wincode;
        }

        public List<Integer> missValues = new ArrayList<>();
        /**
         * 获取彩种当前玩法的遗漏值集合
         * @param missTitle    玩法对应的标签（如11x5 任选：missTitle = "miss"，前一：missTitle = "ge"）
         * @return
         */
        public List<Integer> getMissValues(String missTitle) {

            if (value != null && !value.equals("") && missTitle != null) {
                try {
                    JSONObject obj = new JSONObject(value);
                    if (obj.has(missTitle)) {
                        String missValue = obj.getString(missTitle);
                        missValue =  missValue.subSequence(1, missValue.length()-1).toString();
                        String[] missArray = missValue.split(",");
                        missValues = strZhuanInteger(missArray);
                        for (int i= 0; i < missValues.size(); i++) {
                            if (maxMiss < missValues.get(i)) {
                                maxMiss = missValues.get(i);
                            }
                        }
                    }

                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }


            return missValues;
        }
    }

    /**
     * 获取期号列表
     * @param phaseLength
     * @return
     */
    public List<String> getPhaseInfos(int phaseLength) {
        List<String> phaseInfos = new ArrayList<>();

        if (issues != null && issues.size() > 0) {
            for (int i = 0; i < issues.size(); i++) {
                String phase = issues.get(i).phase;
                if (phase.length() > phaseLength){
                    phase = phase.substring(phase.length()-phaseLength, phase.length());
                }
                phaseInfos.add(phase);
            }
        }
        if (phaseInfos.size() != 0) {
            phaseInfos.add("出现次数");
            phaseInfos.add("平均遗漏");
            phaseInfos.add("最大遗漏");
            phaseInfos.add("最大连出");
        }

        return phaseInfos;
    }

    private static List<Integer> strZhuanInteger(String[] array) {
        List<Integer> missNums = new ArrayList<>();
        if (array != null && array.length > 0) {
            for (int i = 0; i < array.length; i++) {
                int num = 0;
                try {
                    num = Integer.parseInt(array[i]);
                } catch (Exception e) {

                }
                missNums.add(num);
            }
        }
        return missNums;
    }

    public List<List<String>> getMissValues(int payType, int chooseId, String missTitle, String hcTitle) {
        List<List<String>> missValues = new ArrayList<>();

        return missValues;
    }

    protected List<List<String>> getZouShi(String missTitle) {
        List<List<String>> missValues = new ArrayList<>();
        int rowCount = 0;
        if (misses.size() > 0) {
            rowCount = misses.get(0).getMissValues(missTitle).size();
        }
        int[] showCounts = new int[rowCount];
        int[] averageMiss = new int[rowCount];
        int[] maxMiss = new int[rowCount];
        int[] maxLianMiss = new int[rowCount];
        int[] lian = new int[rowCount];
        for (int x = 0; x < lian.length; x++) {
            lian[x] = 1;
        }

        for (int i = 0; i < misses.size(); i++) {
            MissesBean missesBean = misses.get(i);
            List<String> missNums = new ArrayList<>();
            List<Integer> missVs = missesBean.getMissValues(missTitle);
            List<Integer> missVs2 = missVs;
            MissesBean missesBean2;
            if (i > 0) {
                missesBean2 = misses.get(i-1);
                missVs2 = missesBean2.getMissValues(missTitle);
            }

            for (int j = 0; j < missVs.size(); j++) {
                missNums.add(missVs.get(j)+"");

                // 统计
                if (missVs.get(j) == 0) {
                    showCounts[j]++;
                }
                if (i > 0 && missVs.get(j) == 0) {
                    averageMiss[j] += missVs2.get(j);
                }
                if (i == misses.size()-1) {
                    averageMiss[j] += missVs.get(j);
                }
                if (missVs.get(j) > maxMiss[j]) {
                    maxMiss[j] = missVs.get(j);
                }
                if (i > 0) {
                    if (missVs2.get(j) == missVs.get(j)) {
                        lian[j]++;
                    } else {
                        lian[j] = 1;
                    }
                    if (maxLianMiss[j] < lian[j]) {
                        maxLianMiss[j] = lian[j];
                    }
                }
            }

            missValues.add(missNums);
        }

        List<String> showCounts_list = new ArrayList<>();
        List<String> averageMiss_list = new ArrayList<>();
        List<String> maxMiss_list = new ArrayList<>();
        List<String> maxLianMiss_list = new ArrayList<>();

        for (int i = 0; i < showCounts.length; i++) {
            showCounts_list.add(showCounts[i]+"");
            int average;
            if (showCounts[i] == 0) {
                average = maxMiss[i];
            } else {
                Log.d(TAG, "getZouShi: addMiss:"+averageMiss[i]);
                average = averageMiss[i]/(showCounts[i]+1);
//                if (average == 0) {
//                    average = 1;
//                }
            }
            averageMiss_list.add(average+"");
            maxMiss_list.add(maxMiss[i]+"");
            if (showCounts[i] == 0) {
                maxLianMiss[i] = 0;
            }
            maxLianMiss_list.add(maxLianMiss[i]+"");
        }

        missValues.add(showCounts_list);	// 出现次数
        missValues.add(averageMiss_list);	// 平均遗漏
        missValues.add(maxMiss_list);		// 最大遗漏
        missValues.add(maxLianMiss_list);	// 最大连出
        return missValues;
    }

    protected List<String> getOneZouShi(int indext, String missTitle) {
        List<String> missNums = new ArrayList<>();
        MissesBean missesBean = misses.get(indext);
        this.maxMiss[3] = missesBean.maxMiss+"";
        List<Integer> missVs = missesBean.getMissValues(missTitle);

        for (int j = 0; j < missVs.size(); j++) {
            missNums.add(missVs.get(j)+"");

        }
        return missNums;
    }

}
